package com.you.redis.util;

import redis.clients.jedis.Jedis;

/**
 * 使用Jedis连接redis，并且操作redis的指令
 */
public class JedisUtils {

    private static JedisUtils jedisUtils;

    private Jedis jedis = null;

    private final static String REDIS_HOST = "111.229.103.70";

    private final static int REDIS_PORT = 6379;

    private final static String REDIS_PASSWORD = "newFintech@123";

    private JedisUtils() throws Exception {
        if (jedisUtils != null) {
            throw new Exception("不允许new JedisUtils实例对象！");
        }
        jedis = new Jedis(REDIS_HOST, REDIS_PORT);
        jedis.auth(REDIS_PASSWORD); //设置连接密码
        //jedis.select(1); //选择第一个数据库
        String pong = jedis.ping();
        if (pong == null || !("PONG").equals(pong)) {
            throw new Exception("连接redis异常，请检查配合：");
        }
        System.out.println("连接成功，pong=" + pong);
    }

    public static JedisUtils getInstance() throws Exception {
        if (jedisUtils == null) {
            synchronized (JedisUtils.class) {
                if (jedisUtils == null) {
                    jedisUtils = new JedisUtils();
                }
            }
        }
        return jedisUtils;
    }

    public Jedis getJedis() {
        return jedis;
    }

    public static void main(String[] args) throws Exception {
        Jedis jedis = JedisUtils.getInstance().getJedis();
    }
}
